package model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * 
 * @author Alle
 *
 */

@Entity
@Table(name="Behandlinger")
public class Behandling {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	
	private String navn;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Calendar startTid;
	
	@ManyToOne(cascade=CascadeType.ALL)
	@JoinColumn
	private Mellemvare mellemVare;
	
	@OneToMany(cascade=CascadeType.ALL)
	@OrderBy("sort_order ASC")
	@JoinColumn
	private List<DelBehandling> delBehandlinger;
	
	public Behandling() {
		// JPA
	}
	
	public Behandling(String navn, Calendar startTid){
		this.navn = navn;
		this.startTid = startTid;
		this.delBehandlinger = new ArrayList<DelBehandling>();
	}
	
	public int getID() {
		return this.id;
	}
	
	public void setMellemVare(Mellemvare mellemvare){
		setMellemVareUD(mellemvare);
		mellemVare.setBehandling(this);
	}
	
	protected void setMellemVareUD(Mellemvare mellemVare) {
		this.mellemVare = mellemVare;
	}
	
	public void removeMellemvare() {
		this.mellemVare.removeBehandlingUD();
		this.removeMellemvareUD();
		
	}
	
	protected void removeMellemvareUD() {
		this.mellemVare = null;
	}
	
	public Mellemvare getMellemVare() {
		return mellemVare;
	}
	
	public void setNavn(String navn){
		this.navn = navn;
	}
	public String getNavn(){
		return navn;
	}
	
	public void setDelBehandlinger(ArrayList<DelBehandling> delBehandlinger) {
		this.delBehandlinger = delBehandlinger;
	}
	
	public void addDelBehandling(DelBehandling delBehandling) {
		addDelBehandlingUD(delBehandling);
		delBehandling.setBehandlingUD(this);
		delBehandling.setSortOrder(delBehandlinger.size()+1);
	}

	protected void addDelBehandlingUD(DelBehandling delBehandling) {
		this.delBehandlinger.add(delBehandling);
	}
	
	public void removeFirstDelBehandling() {
		if(this.delBehandlinger.size() > 0) {
			this.delBehandlinger.get(0).removeBehandling();
			this.delBehandlinger.remove(0);
		}
	}
	
	public void removeAllDelbehandlinger() {
		for(DelBehandling db : delBehandlinger) {
			db.removeBehandling();
		}
		
		delBehandlinger.clear();
	}
	
	public void setStartTid(Calendar startTid) {
		this.startTid = startTid;
	}

	public Calendar getStartTid() {
		return startTid;
	}
	
	public ArrayList<DelBehandling> getDelbehandlinger(){
		return new ArrayList<DelBehandling>(delBehandlinger);
	}
	
	@Override
	public String toString() {
		return this.navn;
	}
}